<template>
  <div>
    <div id="myChart" :style="{width:'870px',height:'392px'}"></div>
  </div>
</template>

<script>
let echarts = require("echarts/lib/echarts");
require("echarts/lib/chart/bar");
require("echarts/lib/component/tooltip");
require("echarts/lib/component/toolbox");
require("echarts/lib/component/title");

export default {
  props: {
    titleText: {
      type: String
    },
    yAxisData: {
      type: Array
    },
    xAxisData:{
      type: Array,
    }
  },
  watch: {
    yAxisData(newValue, oldValue) {
      this.yAxisData = newValue;
      this.drawBar();
    },
    titleText(newValue, oldValue){
      this.titleText = newValue;
      this.drawBar();
    }
  },
  mounted () {
    this.drawBar();
  },
  methods: {
    drawBar() {
      let chartsContainer = document.getElementById("myChart");
      // 基于准备好的dom，初始化echarts实例
      let myChart = echarts.init(chartsContainer);
      let self = this;
      let option = {
        title: {
          x: "center",
          text: self.titleText,
          textStyle: {
            color: "#333",
            fontSize: 16,
            fontWeight: "normal"
          }
        },
        tooltip: {
          trigger: "item"
        },
        grid: {
          show: true,
          x: 30,
          y: 50,
          x2: 5,
          y2: 50,
          borderWidth: 0
        },
        xAxis: [
          {
            type: "category",
            show: true,
            data: self.xAxisData,
          }
        ],
        yAxis: [
          {
            type: "value",
            show: true,
            splitNumber: 1,
            name: "出次值",
            nameLocation: "end",
            max: function(value) {
              return value.max;
            },
            axisLabel: {
              showMaxLabel: true
            }
          }
        ],
        series: [
          {
            type: "bar",
            itemStyle: {
              normal: {
                color: function(params) {
                  // build a color map as your need.
                  var colorList = [
                    "#E03234",
                    "#8424A0",
                    "#3443A0",
                    "#1793DF",
                    "#17A4B9",
                    "#3F9743",
                    "#B9C339",
                    "#FDAA24",
                    "#F14826",
                    "#4C6570"
                  ];
                  return colorList[params.dataIndex];
                },
                label: {
                  show: true,
                  position: "top"
                }
              }
            },
            data: self.yAxisData,
            barWidth: "100%"
          }
        ]
      };
      myChart.setOption(option);
    }
  }
};
</script>